package org.andolphin.client.model;

import java.io.Serializable;
import java.util.Date;

import org.andolphin.client.utils.DateUtils;
import org.andolphin.client.utils.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class User implements Serializable, JsonSerializable {
    private static final long serialVersionUID = 1L;

    public static final String TAG = User.class.getName();

    private String username;

    private String password;

    private String email;

    private String authToken;

    private Date expired;

    public User() {
    }

    public User(String username, String password, String email, String authToken, Date expired) {
        this.username = username;
        this.password = password;
        this.email = email;
        this.authToken = authToken;
        this.expired = expired;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAuthToken() {
        return authToken;
    }

    public void setAuthToken(String authToken) {
        this.authToken = authToken;
    }

    public Date getExpired() {
        return expired;
    }

    public void setExpired(Date expired) {
        this.expired = expired;
    }

    public void fromJSON(JSONObject json) {
        if (json != null) {
            this.username = json.optString("username");
            this.password = json.optString("password");
            this.email = json.optString("email");
            this.authToken = json.optString("token");
            String dateStr = json.optString("expire");
            this.expired = DateUtils.parseDateTime(dateStr);
        }
    }

    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        try {
            json.put("username", username);
            json.put("password", password);
            json.put("email", email);
            json.put("token", authToken);
            json.put("expire", expired);
            return json;
        } catch (JSONException e) {
            Log.d(TAG, StringUtils.digMessage(e), e);
            return null;
        }
    }

}
